<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model"  slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="账号" :labelCol="labelColThreeFirst" :wrapperCol="wrapperColThreeFirst" >
              <a-input v-model="model.accountNo" placeholder="请输入账号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工号" :labelCol="labelColThree" :wrapperCol="wrapperColThree" >
              <a-input v-model="model.jobNo" placeholder="请输入工号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="姓名" :labelCol="labelColThree" :wrapperCol="wrapperColThree" >
              <a-input v-model="model.userName" placeholder="请输入姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="8">
            <a-form-model-item label="性别" :labelCol="labelColThreeFirst" :wrapperCol="wrapperColThreeFirst" >
              <a-select  v-model="model.gender"  :options="genderOptions" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="生日" :labelCol="labelColThree" :wrapperCol="wrapperColThree">
              <a-date-picker
                style="width: 100%"
                placeholder="请选择生日"
                v-model="model.birthday"
                :format="birthdayFormat"
              />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="8">-->
<!--            <a-form-model-item label="头像" :labelCol="labelColThree" :wrapperCol="wrapperColThree" >-->
<!--              <j-image-upload class="avatar-uploader" bizPath="system" text="上传" v-model="model.avatar" ></j-image-upload>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>


        <a-row>
          <a-col :span="8">
            <a-form-model-item label="职务" :labelCol="labelColThreeFirst" :wrapperCol="wrapperColThreeFirst" >
              <a-select  v-model="model.positionId" style="width: 120px" :options="positionOptions" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="部门" :labelCol="labelColThree" :wrapperCol="wrapperColThree" >
              <a-tree-select
                v-model="model.deptId"
                style="width: 100%"
                :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="deptOptions"
                placeholder="请选择部门"
                tree-default-expand-all
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="角色" :labelCol="labelColThree" :wrapperCol="wrapperColThree" >
              <a-select mode="multiple" v-model="model.roleIds" style="width: 120px" :options="roleOptions" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="8">
            <a-form-model-item label="邮箱" :labelCol="labelColThreeFirst" :wrapperCol="wrapperColThreeFirst" >
              <a-input v-model="model.email" placeholder="请输入邮箱"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="手机号码" :labelCol="labelColThree" :wrapperCol="wrapperColThree" >
              <a-input  v-model="model.mobile" placeholder="请输入"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="座机" :labelCol="labelColThree" :wrapperCol="wrapperColThree" >
              <a-input v-model="model.phone" placeholder="请输入"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

<!--        <a-row>-->
<!--          <a-col :span="8">-->
<!--            <a-form-model-item label="生日" :labelCol="labelColThreeFirst" :wrapperCol="wrapperColThreeFirst">-->
<!--              <a-date-picker-->
<!--                style="width: 100%"-->
<!--                placeholder="请选择生日"-->
<!--                v-model="model.birthday"-->
<!--                format="YYYY-MM-DD"-->
<!--              />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--&lt;!&ndash;          <a-col :span="12">&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash;            <a-form-model-item label="头像" :labelCol="labelColTwo" :wrapperCol="wrapperColTwo" prop="enName">&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash;              <j-image-upload class="avatar-uploader" bizPath="system" text="上传" v-model="model.avatar" ></j-image-upload>&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;&lt;!&ndash;            </a-form-model-item>&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;          </a-col>&ndash;&gt;-->
<!--        </a-row>-->




        <a-row>
          <a-col :span="24">
            <a-form-model-item label="头像" :labelCol="labelColOne" :wrapperCol="wrapperColOne" >
              <j-image-upload class="avatar-uploader" bizPath="system" text="上传" v-model="model.avatar" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>

      </a-form-model>
    </j-form-container>
  </a-spin>
</template>


<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import JUpload from '@/components/jeecg/JUpload'
  import { FormMixin } from '@/mixins/FormMixin'
  import { getDataOptionList } from '../../../utils/util'
  import { requestSetData, requestSetOptions } from '../../../utils/commonUtils'
  import moment from 'moment'

  export default {
    name: 'UserForm',
    mixins: [ FormMixin],
    components: {
      ATextarea
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        positionOptions:[],
        roleOptions:[],
        deptOptions:[],
        genderOptions:getDataOptionList('gender'),
        birthdayFormat:"YYYY-MM-DD",
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/user/employee/save",
          edit: "/user/employee/update",
          queryById: "/user/employee/detail"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      let $this = this;
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));

      requestSetOptions("/user/position/listAll",{},function(options){$this.positionOptions=options;})
      requestSetData("/user/dept/listTree",{},function(options){$this.deptOptions=options;})
      requestSetOptions("/user/role/listAll",{},function(options){$this.roleOptions=options;})

      // getAction("/user/dataSource/listAll",{}).then(res=>{
      //   if(res.success){
      //     this.dataSourceOptions = res.data.map((item,index,arr)=>{
      //       let c = {label:item.name, value: item.id}
      //       return c;
      //     });
      //     console.log("dataSourceOptions="+this.dataSourceOptions);
      //   }
      // });
      // getAction("/user/template/listAll",{}).then(res=>{
      //   if(res.success){
      //     this.templateOptions = res.data.map((item,index,arr)=>{
      //       let c = {label:item.name, value: item.id}
      //       return c;
      //     });
      //     console.log("templateOptions="+this.templateOptions);
      //   }
      // });

    },
    methods: {
      moment,
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        // this.model.dataSource = this.model.dataSource || {id:null};
        // this.model.template = this.model.template || {id:null};
        this.model.birthday = moment(this.model.birthday).format('YYYY-MM-DD')
        this.visible = true;
      },
      handleChange(value) {
        console.log(`selected ${value}`);
      },
      onChange(e) {
        console.log('radio1 checked', e.target.value);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            if(!this.model.id){
              httpurl+=this.url.add;
            }else{
              httpurl+=this.url.edit;
            }
            httpAction(httpurl,this.model,"post").then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>